/* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License.  You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/


/*************************************************************************
 *\author: starlove
 *************************************************************************/
#ifndef __CC_PYTHON_IMPORT_O_CACHES_IMPL__
#define __CC_PYTHON_IMPORT_O_CACHES_IMPL__

#include "python_object_caches.h"

NS_CCPY_BEGIN
	PYCLASS_INIT_AND_BASE(CPyAnimationCache, CPyCCObject, "sharedAnimationCache", "Python sharedAnimationCache Object");
	PYCLASS_BEGIN_METHOD_DEF(CPyAnimationCache)
		PYCLASS_ADD_METHOD_EX(CPyAnimationCache, purgeSharedAnimationCache	, METH_STATIC|METH_VARARGS)
		PYCLASS_ADD_METHOD_EX(CPyAnimationCache, addAnimation				, METH_STATIC|METH_VARARGS)
		PYCLASS_ADD_METHOD_EX(CPyAnimationCache, removeAnimationByName		, METH_STATIC|METH_VARARGS)
		PYCLASS_ADD_METHOD_EX(CPyAnimationCache, animationByName			, METH_STATIC|METH_VARARGS)
		PYCLASS_ADD_METHOD_EX(CPyAnimationCache, addAnimationsWithFile		, METH_STATIC|METH_VARARGS)
	PYCLASS_END_METHOD_DEF(CPyAnimationCache)
	PYCLASS_BEGIN_GETSET_DEF(CPyAnimationCache)
	PYCLASS_END_GETSET_DEF(CPyAnimationCache)
	PYCLASS_IMP_INTERFACE_NEW(CPyAnimationCache)
	
	int CPyAnimationCache::__init__(CPyAnimationCache *pySelf, PyObject *args, PyObject *kwds)
	{
		return 0;
	}

	void CPyAnimationCache::__del__(CPyAnimationCache *self)
	{
		self->ob_type->tp_free(self);
	}

	PYCLASS_METHOD_IMPL(CPyAnimationCache, purgeSharedAnimationCache)
	{
		cocos2d::CCAnimationCache::purgeSharedAnimationCache();
		Py_RETURN_NONE;
	}

	PYCLASS_METHOD_IMPL(CPyAnimationCache, addAnimation)
	{
		PyObject *pyani = NULL;
		char *name = NULL;
		if( !PyArg_ParseTuple(pyArgs, "Os", &pyani, &name) )
			Py_RETURN_NONE;
		if(!name || !pyani || !PyObject_TypeCheck(pyani, CPyClassBase::GetTypeObject<CPyAnimation>()))
			Py_RETURN_NONE;
		CPyAnimation *ani = (CPyAnimation *)pyani;
		cocos2d::CCAnimationCache::sharedAnimationCache()->addAnimation((cocos2d::CCAnimation *)ani->getCCObject(), name);
		Py_RETURN_NONE;
	}

	PYCLASS_METHOD_IMPL(CPyAnimationCache, removeAnimationByName)
	{
		char *name = NULL;
		if( !PyArg_ParseTuple(pyArgs, "s", &name) )
			Py_RETURN_NONE;
		if(!name)
			Py_RETURN_NONE;
		cocos2d::CCAnimationCache::sharedAnimationCache()->removeAnimationByName(name);
		Py_RETURN_NONE;
	}

	PYCLASS_METHOD_IMPL(CPyAnimationCache, animationByName)
	{
		char *name = NULL;
		if( !PyArg_ParseTuple(pyArgs, "s", &name) )
			Py_RETURN_NONE;
		if(!name)
			Py_RETURN_NONE;
		cocos2d::CCAnimation *ani = cocos2d::CCAnimationCache::sharedAnimationCache()->animationByName(name);
		if(!ani)
			Py_RETURN_NONE;
		return (PyObject *)CPyCCObject::create<CPyAnimation>(ani);
	}

	PYCLASS_METHOD_IMPL(CPyAnimationCache, addAnimationsWithFile)
	{
		char *name = NULL;
		if( !PyArg_ParseTuple(pyArgs, "s", &name) )
			Py_RETURN_NONE;
		if(!name)
			Py_RETURN_NONE;
		cocos2d::CCAnimationCache::sharedAnimationCache()->addAnimationsWithFile(name);
		Py_RETURN_NONE;
	}

	/////////////////////////////////////////////////////////////////
	PYCLASS_INIT_AND_BASE(CPySpriteFrameCache, CPyCCObject, "sharedSpriteFrameCache", "Python sharedSpriteFrameCache Object");
	PYCLASS_BEGIN_METHOD_DEF(CPySpriteFrameCache)
		PYCLASS_ADD_METHOD_EX(CPySpriteFrameCache, purgeSharedSpriteFrameCache		, METH_STATIC|METH_VARARGS)
		PYCLASS_ADD_METHOD_EX(CPySpriteFrameCache, addSpriteFramesWithFile			, METH_STATIC|METH_KEYWORDS|METH_VARARGS)
		PYCLASS_ADD_METHOD_EX(CPySpriteFrameCache, addSpriteFrame					, METH_STATIC|METH_VARARGS)
		PYCLASS_ADD_METHOD_EX(CPySpriteFrameCache, removeSpriteFrames				, METH_STATIC|METH_VARARGS)
		PYCLASS_ADD_METHOD_EX(CPySpriteFrameCache, removeUnusedSpriteFrames			, METH_STATIC|METH_VARARGS)
		PYCLASS_ADD_METHOD_EX(CPySpriteFrameCache, removeSpriteFrameByName			, METH_STATIC|METH_VARARGS)
		PYCLASS_ADD_METHOD_EX(CPySpriteFrameCache, removeSpriteFramesFromFile		, METH_STATIC|METH_VARARGS)
		PYCLASS_ADD_METHOD_EX(CPySpriteFrameCache, removeSpriteFramesFromTexture	, METH_STATIC|METH_VARARGS)
		PYCLASS_ADD_METHOD_EX(CPySpriteFrameCache, spriteFrameByName				, METH_STATIC|METH_VARARGS)
	PYCLASS_END_METHOD_DEF(CPySpriteFrameCache)
	PYCLASS_BEGIN_GETSET_DEF(CPySpriteFrameCache)
	PYCLASS_END_GETSET_DEF(CPySpriteFrameCache)
	PYCLASS_IMP_INTERFACE_NEW(CPySpriteFrameCache)
	
	int CPySpriteFrameCache::__init__(CPySpriteFrameCache *pySelf, PyObject *args, PyObject *kwds)
	{
		return 0;
	}

	void CPySpriteFrameCache::__del__(CPySpriteFrameCache *self)
	{
		self->ob_type->tp_free(self);
	}

	PYCLASS_METHOD_IMPL(CPySpriteFrameCache, purgeSharedSpriteFrameCache)
	{
		cocos2d::CCSpriteFrameCache::purgeSharedSpriteFrameCache();
		Py_RETURN_NONE;
	}
		
	PYCLASS_METHOD_KW_IMPL(CPySpriteFrameCache, addSpriteFramesWithFile)
	{
		if( !pyArgs )
			Py_RETURN_NONE;
		static char* kwlist[] = {"plist", "textureFileName", "texture", NULL};
		char *plist = NULL;
		char *textureFileName = NULL;
		PyObject *texture = NULL;
		if(!PyArg_ParseTupleAndKeywords(pyArgs, pyKW, "s|sO", kwlist, &plist, &textureFileName, &texture))
			Py_RETURN_FALSE;
		if(!plist)
			Py_RETURN_FALSE;
		if(texture && PyObject_TypeCheck(texture, CPyClassBase::GetTypeObject<CPyTexture2D>()))
		{
			CPyTexture2D *tex = (CPyTexture2D *)texture;
			cocos2d::CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile(plist, (cocos2d::CCTexture2D *)tex->getCCObject());
		}else if(textureFileName)
		{
			cocos2d::CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile(plist, textureFileName);
		}else
			cocos2d::CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile(plist);
		
		Py_RETURN_NONE;
	}

	PYCLASS_METHOD_IMPL(CPySpriteFrameCache, addSpriteFrame)
	{
		PyObject *O = NULL;
		char *name = NULL;
		if( !PyArg_ParseTuple(pyArgs, "Os", &O, &name) )
			Py_RETURN_NONE;
		if(!name || !O || !PyObject_TypeCheck(O, CPyClassBase::GetTypeObject<CPySpriteFrame>()))
			Py_RETURN_NONE;
		CPySpriteFrame *frm = (CPySpriteFrame *)O;
		cocos2d::CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFrame((cocos2d::CCSpriteFrame *)frm->getCCObject(), name);
		Py_RETURN_NONE;
	}

	PYCLASS_METHOD_IMPL(CPySpriteFrameCache, removeSpriteFrames)
	{
		cocos2d::CCSpriteFrameCache::sharedSpriteFrameCache()->removeSpriteFrames();
		Py_RETURN_NONE;
	}

	PYCLASS_METHOD_IMPL(CPySpriteFrameCache, removeUnusedSpriteFrames)
	{
		cocos2d::CCSpriteFrameCache::sharedSpriteFrameCache()->removeUnusedSpriteFrames();
		Py_RETURN_NONE;
	}

	PYCLASS_METHOD_IMPL(CPySpriteFrameCache, removeSpriteFrameByName)
	{
		char *name = NULL;
		if( !PyArg_ParseTuple(pyArgs, "s", &name) )
			Py_RETURN_NONE;
		if(!name)
			Py_RETURN_NONE;
		cocos2d::CCSpriteFrameCache::sharedSpriteFrameCache()->removeSpriteFrameByName(name);
		Py_RETURN_NONE;
	}
	PYCLASS_METHOD_IMPL(CPySpriteFrameCache, removeSpriteFramesFromFile)
	{
		char *name = NULL;
		if( !PyArg_ParseTuple(pyArgs, "s", &name) )
			Py_RETURN_NONE;
		if(!name)
			Py_RETURN_NONE;
		cocos2d::CCSpriteFrameCache::sharedSpriteFrameCache()->removeSpriteFramesFromFile(name);
		Py_RETURN_NONE;
	}
	PYCLASS_METHOD_IMPL(CPySpriteFrameCache, removeSpriteFramesFromTexture)
	{
		PyObject *texture = NULL;
		if( !PyArg_ParseTuple(pyArgs, "O", &texture) )
			Py_RETURN_NONE;
		if(!texture && !PyObject_TypeCheck(texture, CPyClassBase::GetTypeObject<CPyTexture2D>()))
			Py_RETURN_NONE;
		CPyTexture2D *tex = (CPyTexture2D *)texture;
		cocos2d::CCSpriteFrameCache::sharedSpriteFrameCache()->removeSpriteFramesFromTexture((cocos2d::CCTexture2D *)tex->getCCObject());
		Py_RETURN_NONE;
	}

	PYCLASS_METHOD_IMPL(CPySpriteFrameCache, spriteFrameByName)
	{
		char *name = NULL;
		if( !PyArg_ParseTuple(pyArgs, "s", &name) )
			Py_RETURN_NONE;
		if(!name)
			Py_RETURN_NONE;
		cocos2d::CCSpriteFrame *frm = cocos2d::CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(name);
		if(!frm)
			Py_RETURN_NONE;
		PyObject *p = (PyObject *)CPyCCObject::create<CPySpriteFrame>(frm);
		return p;
	}
	
	/////////////////////////////////////////////////////////////////
	PYCLASS_INIT_AND_BASE(CPyTextureCache, CPyCCObject, "sharedTextureCache", "Python sharedTextureCache Object");
	PYCLASS_BEGIN_METHOD_DEF(CPyTextureCache)
		PYCLASS_ADD_METHOD_EX(CPyTextureCache, purgeSharedTextureCache	, METH_STATIC|METH_VARARGS)
		PYCLASS_ADD_METHOD_EX(CPyTextureCache, addImage					, METH_STATIC|METH_VARARGS)
		PYCLASS_ADD_METHOD_EX(CPyTextureCache, addImageAsync			, METH_STATIC|METH_VARARGS)
		PYCLASS_ADD_METHOD_EX(CPyTextureCache, addUIImage				, METH_STATIC|METH_VARARGS)
		PYCLASS_ADD_METHOD_EX(CPyTextureCache, textureForKey			, METH_STATIC|METH_VARARGS)
		PYCLASS_ADD_METHOD_EX(CPyTextureCache, removeAllTextures		, METH_STATIC|METH_VARARGS)
		PYCLASS_ADD_METHOD_EX(CPyTextureCache, removeUnusedTextures		, METH_STATIC|METH_VARARGS)
		PYCLASS_ADD_METHOD_EX(CPyTextureCache, removeTexture			, METH_STATIC|METH_VARARGS)
		PYCLASS_ADD_METHOD_EX(CPyTextureCache, removeTextureForKey		, METH_STATIC|METH_VARARGS)
		PYCLASS_ADD_METHOD_EX(CPyTextureCache, dumpCachedTextureInfo	, METH_STATIC|METH_VARARGS)
		PYCLASS_ADD_METHOD_EX(CPyTextureCache, addPVRImage				, METH_STATIC|METH_VARARGS)
		PYCLASS_ADD_METHOD_EX(CPyTextureCache, reloadAllTextures		, METH_STATIC|METH_VARARGS)
	PYCLASS_END_METHOD_DEF(CPyTextureCache)
	PYCLASS_BEGIN_GETSET_DEF(CPyTextureCache)
	PYCLASS_END_GETSET_DEF(CPyTextureCache)
	PYCLASS_IMP_INTERFACE_NEW(CPyTextureCache)
	
	int CPyTextureCache::__init__(CPyTextureCache *pySelf, PyObject *args, PyObject *kwds)
	{
		return 0;
	}

	void CPyTextureCache::__del__(CPyTextureCache *self)
	{
		self->ob_type->tp_free(self);
	}

	PYCLASS_METHOD_IMPL(CPyTextureCache, purgeSharedTextureCache)
	{
		cocos2d::CCTextureCache::purgeSharedTextureCache();
		Py_RETURN_NONE;
	}

	PYCLASS_METHOD_IMPL(CPyTextureCache, addImage)
	{
		char *name = NULL;
		if( !PyArg_ParseTuple(pyArgs, "s", &name) )
			Py_RETURN_NONE;
		if(!name)
			Py_RETURN_NONE;
		cocos2d::CCTexture2D *tex = cocos2d::CCTextureCache::sharedTextureCache()->addImage(name);
		if(!tex)
			Py_RETURN_NONE;
		return (PyObject *)CPyCCObject::create<CPyTexture2D>(tex);
	}

	PYCLASS_METHOD_IMPL(CPyTextureCache, addImageAsync)
	{
		char *name = NULL;
		PyObject *fn = NULL;
		if( !PyArg_ParseTuple(pyArgs, "sO", &name, &fn) )
			Py_RETURN_NONE;
		if(!name || !fn || !PyCallable_Check(fn))
			Py_RETURN_NONE;
		Py_XINCREF(fn);
		CallFuncDelegate *delg = CallFuncDelegate::create(fn);
		cocos2d::CCTextureCache::sharedTextureCache()->addImageAsync(name, delg, __callfuncO_selector(CallFuncDelegate::On_SEL_CallFuncO));
		Py_RETURN_NONE;
	}

	PYCLASS_METHOD_IMPL(CPyTextureCache, addUIImage)
	{
		char *name = NULL;
		PyObject *img = NULL;
		if( !PyArg_ParseTuple(pyArgs, "Os", &img, &name) )
			Py_RETURN_NONE;
		if(!name || !img || PyObject_TypeCheck(img, CPyClassBase::GetTypeObject<CPyImage>()))
			Py_RETURN_NONE;
		CPyImage *pimg = (CPyImage *)img;
		cocos2d::CCTexture2D *tex = cocos2d::CCTextureCache::sharedTextureCache()->addUIImage((cocos2d::CCImage *)pimg->getCCObject(), name);
		if(!tex)
			Py_RETURN_NONE;
		return (PyObject *)CPyCCObject::create<CPyTexture2D>(tex);
	}

	PYCLASS_METHOD_IMPL(CPyTextureCache, textureForKey)
	{
		char *name = NULL;
		if( !PyArg_ParseTuple(pyArgs, "s", &name) )
			Py_RETURN_NONE;
		if(!name)
			Py_RETURN_NONE;
		cocos2d::CCTexture2D *tex = cocos2d::CCTextureCache::sharedTextureCache()->textureForKey(name);
		if(!tex)
			Py_RETURN_NONE;
		return (PyObject *)CPyCCObject::create<CPyTexture2D>(tex);
	}

	PYCLASS_METHOD_IMPL(CPyTextureCache, removeAllTextures)
	{
		cocos2d::CCTextureCache::sharedTextureCache()->removeAllTextures();
		Py_RETURN_NONE;
	}

	PYCLASS_METHOD_IMPL(CPyTextureCache, removeUnusedTextures)
	{
		cocos2d::CCTextureCache::sharedTextureCache()->removeUnusedTextures();
		Py_RETURN_NONE;
	}

	PYCLASS_METHOD_IMPL(CPyTextureCache, removeTexture)
	{
		PyObject *tex = NULL;
		if( !PyArg_ParseTuple(pyArgs, "O", &tex) )
			Py_RETURN_NONE;
		if(!tex || PyObject_TypeCheck(tex, CPyClassBase::GetTypeObject<CPyTexture2D>()))
			Py_RETURN_NONE;
		CPyTexture2D *ptex = (CPyTexture2D *)tex;
		cocos2d::CCTextureCache::sharedTextureCache()->removeTexture((cocos2d::CCTexture2D *)ptex->getCCObject());
		Py_RETURN_NONE;
	}

	PYCLASS_METHOD_IMPL(CPyTextureCache, removeTextureForKey)
	{
		char *name = NULL;
		if( !PyArg_ParseTuple(pyArgs, "s", &name) )
			Py_RETURN_NONE;
		if(!name)
			Py_RETURN_NONE;
		cocos2d::CCTextureCache::sharedTextureCache()->removeTextureForKey(name);
		Py_RETURN_NONE;
	}

	PYCLASS_METHOD_IMPL(CPyTextureCache, dumpCachedTextureInfo)
	{
		cocos2d::CCTextureCache::sharedTextureCache()->dumpCachedTextureInfo();
		Py_RETURN_NONE;
	}

	PYCLASS_METHOD_IMPL(CPyTextureCache, addPVRImage)
	{
		char *name = NULL;
		if( !PyArg_ParseTuple(pyArgs, "s", &name) )
			Py_RETURN_NONE;
		if(!name)
			Py_RETURN_NONE;
		cocos2d::CCTexture2D *tex = cocos2d::CCTextureCache::sharedTextureCache()->addPVRImage(name);
		if(!tex)
			Py_RETURN_NONE;
		return (PyObject *)CPyCCObject::create<CPyTexture2D>(tex);
	}

	PYCLASS_METHOD_IMPL(CPyTextureCache, reloadAllTextures)
	{
		cocos2d::CCTextureCache::sharedTextureCache()->reloadAllTextures();
		Py_RETURN_NONE;
	}


NS_CCPY_END

#endif